Merge Sort Visualization

OVERVIEW


Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It works by recursively dividing the input array into smaller subarrays and sorting those subarrays then merging them back together to obtain the sorted array.

Working


  • Divide: Divide the list or array recursively into two halves until it can no more be divided.

  • Conquer: Each subarray is sorted individually using the merge sort algorithm.

  • Merge: The sorted subarrays are merged back together in sorted order. The process continues until all elements from both subarrays have been merged.

Complexity


  • Time Complexity: O(nlogn)

    Reason: At each step, we divide the whole array, for that logn and we assume n steps are taken to get sorted array, so overall time complexity will be nlogn.

  • Space Complexity: O(n)

    Reason: We are using a temporary array to store elements in sorted order.

  • Auxiliary Space Complexity: O(n)